package net

import (
	"fmt"
	"net"
	"rpc-demo/handler"
	"strconv"
)

type NexusServer[T any] struct {
	Port       int
	Rhd        readHandler[T]
	Sr         string
	chanelList []*NexusChanel[T]
}

func (server NexusServer[T]) StartServer() {
	listen, err := net.Listen(TYPE, ":"+strconv.Itoa(server.Port))
	if handler.ErrorHandler(err) {
		return
	}
	fmt.Printf("服务端已启动 port：%d\n", server.Port)
	for {
		conn, err := listen.Accept()
		if handler.ErrorHandler(err) {
			break
		}
		chanel := NexusChanel[T]{
			conn: conn,
			sr:   getSr[T](server.Sr),
		}
		server.chanelList = append(server.chanelList, &chanel)
		go chanel.handleRead(server.Rhd)
	}
}

type readHandler[T any] func(obj T, chanel *NexusChanel[T], err error)
